package com.loippi.core.gen.processor;

import com.loippi.core.gen.utils.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
/**
 * @Description:  覆盖代码生成器方法
 * @author:    wangcj
 * @date:    2017年6月7日 上午10:08:41 
 * @version    V1.0   
 */
public class Generator
{
  private static void generate(Map<String, Object> data, String path, String fileName, String templateName)
  {
    try
    {
      Configuration cfg = FreemarkerConfig.getConfiguration();
      Template template = cfg.getTemplate(templateName);
      File file = new File(path + fileName);
      File directory = file.getParentFile();
      if (!(directory.exists())) {
        directory.mkdirs();
      }
      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
      template.process(data, out);
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private static String generate(Map<String, Object> data, String templateName)
  {
    try
    {
      Configuration cfg = FreemarkerConfig.getConfiguration();
      Template template = cfg.getTemplate(templateName);
      StringWriter out = new StringWriter();
      template.process(data, out);
      return out.toString();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  private static void initGenerateConfig(Map<String, Object> data, String className) {
    data.put("classNamePath", StringUtils.camelToUnderline(className));
    data.put("GenericDao", "com.framework.loippi.mybatis.dao.GenericDao");
    data.put("GenericEntity", "com.framework.loippi.mybatis.eitity.GenericEntity");
    data.put("GenericService", "com.framework.loippi.service.GenericService");
    data.put("GenericServiceImpl", "com.framework.loippi.service.impl.GenericServiceImpl");
    data.put("GenericController", "com.framework.loippi.controller.admin.GenericController");
    data.put("current_version", "2.0");
  }

  public static String runPostman(Map<String, Object> data) {
    return generate(data, "postman.ftl");
  }

  public static void runParams(String path, String packageName, Map<String, Object> data, String functionName) {
    data.put("packageName", packageName.toLowerCase());
    generate(data, path + "/" + packageName + "/com/framework/loippi/controller/api/params/" + packageName.toLowerCase() + "/", functionName.concat("Param.java"), "params.ftl");
  }

  public static void runResult(String path, String packageName, Map<String, Object> data, String functionName) {
    data.put("packageName", packageName.toLowerCase());
    generate(data, path + "/" + packageName + "/com/framework/loippi/controller/api/result/" + packageName.toLowerCase() + "/", functionName.concat("Result.java"), "results.ftl");
  }

  public static void runAPI(String path, String packageName, Map<String, Object> data) {
    generate(data, path + "/" + packageName + "/com/framework/loippi/controller/api/", StringUtils.upperFirst(packageName).concat("APIController.java"), "api.ftl");
  }

  public static void runEntity(Map<String, Object> data, String path, String className, String packageName)
  {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/entity/", className.concat(".java"), "entity.ftl");
  }

  public static void runDao(Map<String, Object> data, String path, String className, String packageName)
  {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/dao/", className.concat("Dao.java"), "dao.ftl");
  }

  public static void runMapper(Map<String, Object> data, String path, String className, String packageName) {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/mappers/", className.concat("Dao.xml"), "mapper.ftl");
  }

  public static void runService(Map<String, Object> data, String path, String className, String packageName)
  {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/service/", className.concat("Service.java"), "service.ftl");
  }

  public static void runServiceImpl(Map<String, Object> data, String path, String className, String packageName) {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/service/impl/", className.concat("ServiceImpl.java"), "service_impl.ftl");
  }

  public static void runController(Map<String, Object> data, String path, String className, String packageName) {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/" + packageName.replace(".", "/") + "/controller/admin/", className.concat("Controller.java"), "controller.ftl");
  }

  public static void runView(Map<String, Object> data, String path, String className, String packageName)
  {
    initGenerateConfig(data, className);
    generate(data, path + "/" + className.toString().toLowerCase() + "/template/admin/" + StringUtils.camelToUnderline(className) + "/", "add.html", "add.ftl");
    generate(data, path + "/" + className.toString().toLowerCase() + "/template/admin/" + StringUtils.camelToUnderline(className) + "/", "edit.html", "edit.ftl");
    generate(data, path + "/" + className.toString().toLowerCase() + "/template/admin/" + StringUtils.camelToUnderline(className) + "/", "view.html", "view.ftl");
    generate(data, path + "/" + className.toString().toLowerCase() + "/template/admin/" + StringUtils.camelToUnderline(className) + "/", "list.html", "list.ftl");
  }
}